package com.daatak.mvc.finance;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.daatak.finance.business.FinancialBeanI;
import com.daatak.finance.domain.FinancialProductObject;
import com.daatak.finance.domain.RateBandObject;
import com.daatak.finance.domain.RateCardObject;
import com.daatak.mvc.common.JsonResponse;

@Controller
public class FinancialsController {
	
	@Autowired
	FinancialBeanI financialBean;
	
	@InitBinder
	public void initBinder(WebDataBinder webDataBinder) {
	 SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
	 dateFormat.setLenient(false);
	 webDataBinder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, true));
	 }
	
	
	@RequestMapping(value="/finance", method = RequestMethod.GET)
	public String financials(ModelMap model,HttpServletRequest request) {
		return "ajax.finance/financialGrid";
	}
	@RequestMapping(value="/finance/home", method = RequestMethod.GET)
	public String financialsHome(ModelMap model,HttpServletRequest request) {
		return "home.finance/financialGrid";
	}
	
	@RequestMapping(value="/finance/financialGrid", method = RequestMethod.GET)
	public @ResponseBody List<FinancialProductObject> financialList(ModelMap model,HttpServletRequest request) {
		
		return financialBean.list();
	}
	
	
	@RequestMapping(value="/finance/getInputScreen", method = RequestMethod.GET)
	public String getInputScreen(ModelMap model) {
		
		FinancialPojo pojo = new FinancialPojo();
		model.addAttribute("financial",pojo);
		
		return "ajax.finance/financialsMainform";
	}
	
	@RequestMapping(value="/finance/getInputScreen/{id}", method = RequestMethod.GET)
	public String getInputScreen(@PathVariable("id") Integer Id, ModelMap model) {
		
		FinancialPojo financialPojo = new FinancialPojo();
		financialPojo.setFinancial(financialBean.findById(Id));
		
		model.addAttribute("financial",financialPojo);
		model.addAttribute("financial",financialPojo);
		
		return "ajax.finance/financialsMainform";
	}
	
	@RequestMapping(value="/finance/miniForm", method = RequestMethod.GET)
	public String financialFormMini(@ModelAttribute FinancialPojo financialPojo, ModelMap model) {

		
		financialPojo.setFinancial(financialBean.findById(financialPojo.getFinancial().getFinancialProductId()));
		
		model.addAttribute("financial",financialPojo);
		
		return "ajax.finance/financialForm";
	}
	
	@RequestMapping(value="/finance/add", method = RequestMethod.POST)
	public @ResponseBody JsonResponse add(@ModelAttribute FinancialPojo financialPojo, BindingResult result, ModelMap model) {
		
		
		FinancialValidator validator = new FinancialValidator();
		validator.validate(financialPojo, result);
		JsonResponse res = new JsonResponse();
		
		if(result.hasErrors()){
			 res.setFieldError(result.getFieldErrors());
			  res.setStatus("ERROR");
		}
		else
		{
			financialBean.createFinancialProduct(financialPojo.getFinancial());
			res.setStatus("SUCCESS");
			res.setObjectName("financial.financialProductId");
			res.setObjectId(String.valueOf(financialPojo.getFinancial().getFinancialProductId()));
		}
		
		
		return res;
	}
	@RequestMapping(value="/finance/details/{id}", method = RequestMethod.GET)
	public String financialDetails(@PathVariable("id")Integer Id,ModelMap model) {
		FinancialPojo pojo = new FinancialPojo();
		
		pojo.setFinancial(financialBean.findById(Id));
		
		model.addAttribute("financial", pojo.getFinancial());
		
		return "ajax.finance/financialDetails";
	}
	
	@RequestMapping(value="/finance/edit", method = RequestMethod.POST)
	public String editFinancial(@RequestParam(value="id",required=false,defaultValue="0") Integer id,ModelMap model) {
		
		FinancialPojo pojo = new FinancialPojo();
		
		pojo.setFinancial(financialBean.findById(id));
		
		model.addAttribute("financial", pojo);
		return "ajax.finance/financialsMainform";
	}
	@RequestMapping(value="/finance/ratecard/input", method = RequestMethod.POST)
	public String addRateCard(@ModelAttribute FinancialPojo financialPojo, ModelMap model) {
		
		
		model.addAttribute("financial",financialPojo);
		
		return "ajax.finance/ratecardForm";
	}
	
	@RequestMapping(value="/finance/ratecards/json", method = RequestMethod.GET)
	public @ResponseBody List<RateCardObject> rateCardAsJson(@ModelAttribute FinancialPojo financialPojo, ModelMap model) {
		return financialBean.listRateCards(financialPojo.getFinancial().getFinancialProductId());
	}
	
	@RequestMapping(value="/finance/ratebands/json", method = RequestMethod.GET)
	public @ResponseBody List<RateBandObject> rateBandsAsJson(@ModelAttribute FinancialPojo financialPojo, ModelMap model) {
		return financialBean.listRateBand(financialPojo.getRatecard().getRateCardId(), financialPojo.getFinancial().getFinancialProductId());
	}
	
	@RequestMapping(value="/finance/ratecard/add", method = RequestMethod.POST)
	public @ResponseBody JsonResponse addRateCard1(@ModelAttribute FinancialPojo financialPojo,BindingResult result,ModelMap model) {
		
		FinancialValidator validator = new FinancialValidator();
		validator.validateRateCard(financialPojo, result);
		JsonResponse res = new JsonResponse();
		
		if(result.hasErrors()){
			 res.setFieldError(result.getFieldErrors());
			  res.setStatus("ERROR");
		}
		else{
				financialBean.addRateCard(financialPojo.getRatecard());
				 res.setStatus("SUCCESS");
		}
		
		return res;
	}
	
	@RequestMapping(value="/finance/rateband/input", method = RequestMethod.POST)
	public String addRateBand(@ModelAttribute FinancialPojo financialPojo, ModelMap model) {
		
		//FinancialPojo pojo = new FinancialPojo();
		model.addAttribute("financial",financialPojo);
		
		return "ajax.finance/ratebandForm";
	}
	
	@RequestMapping(value="/finance/rateband/add", method = RequestMethod.POST)
	public @ResponseBody JsonResponse addRateRate(@ModelAttribute FinancialPojo financialPojo,BindingResult result,ModelMap model) {
		
		FinancialValidator validator = new FinancialValidator();
		validator.validateRateBand(financialPojo, result);
		JsonResponse res = new JsonResponse();
		
		if(result.hasErrors()){
			 res.setFieldError(result.getFieldErrors());
			 res.setStatus("ERROR");
		}
		else{
		//financialBean.addRateBand(financialPojo.getRateband());
		res.setStatus("SUCCESS");
		}
		
		return res;
	}
	
	@RequestMapping(value="/finance/calculator", method = RequestMethod.POST)
	public String showCalculationWidget(ModelMap model) {
		
		
		/*FinancialPojo pojo = new FinancialPojo();
		model.addAttribute("financial",pojo);
		model.addAttribute("financialProducts1", financialBean.listFinancialProduct());
		System.out.println("financialProducts"+ financialBean.listFinancialProduct());*/
		
		return "ajax.finance/financialCalculator";
	}
	
	@RequestMapping(value="/finance/calculator/calculate", method = RequestMethod.POST)
	public @ResponseBody String calcuateFianancedAmount(@ModelAttribute FinancialCalculatorPojo financialPojo,ModelMap model) {
		model.addAttribute("financial", financialPojo);
		return "ajax.finance/rateCardBand";
	}


}
